package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.domain.ActivityRemark;
import com.bjpowernode.crm.workbench.service.ActivityRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class ActivityDetailController {
    @Autowired
    private ActivityRemarkService activityRemarkService;

    @RequestMapping(value = "/workbench/activity/saveCreateActivityRemark.do")
    @ResponseBody
    public Object saveCreateActivityRemark(String remark, String activityId, HttpSession session){
        ReturnObject returnObject = new ReturnObject();

        User user = (User)session.getAttribute(Contants.SESSION_USER);

        ActivityRemark activityRemark = new ActivityRemark();
        activityRemark.setId(UUIDUtils.getUUID());
        activityRemark.setActivityId(activityId);
        activityRemark.setCreateBy(user.getId());
        activityRemark.setCreateTime(DateUtils.formatDataTime(new Date()));
        activityRemark.setNoteContent(remark);

        try {
            int res = activityRemarkService.saveCreateActivityRemark(activityRemark);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                activityRemark.setCreateBy(user.getName());
                returnObject.setRetData(activityRemark);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }

    @RequestMapping(value = "/workbench/activity/deleteActivityRemarkById.do")
    @ResponseBody
    public Object deleteActivityRemarkById(String id){
        ReturnObject returnObject = new ReturnObject();

        try {
            int res = activityRemarkService.deleteActivityRemarkById(id);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }

    @RequestMapping(value = "/workbench/activity/updateActivityRemarkById.do")
    @ResponseBody
    public Object updateActivityRemarkById(String id,String remark,HttpSession session){
        ReturnObject returnObject = new ReturnObject();

        User user = (User)session.getAttribute(Contants.SESSION_USER);

        ActivityRemark activityRemark = new ActivityRemark();
        activityRemark.setId(id);
        activityRemark.setNoteContent(remark);
        activityRemark.setEditFlag("1");
        activityRemark.setEditBy(user.getId());
        activityRemark.setEditTime(DateUtils.formatDataTime(new Date()));

        try {
            int res = activityRemarkService.updateActivityRemarkById(activityRemark);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                activityRemark.setEditBy(user.getName());
                returnObject.setRetData(activityRemark);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }
}
